__author__ = 'wangjc'

from PageObjects.BasePage import BasePage
from widgets.swipe import Swipe
import time
import os
from selenium.common.exceptions import NoSuchElementException

class Widget(BasePage):

    def __init__(self, driver):
        self.driver = driver

    # 文本框输入
    def input(self, input_text,widgetVal,widgetType,assertVal):
        # 等待手机号输入框可见
        # cons = self.driver.contexts
        func = self.get_element_by_common(input_text,widgetType)
        func.send_keys(widgetVal)
        # 关闭虚拟键盘
        os.popen("adb shell input keyevent 66")

    # 根据id滑动页面并选择
    def swipe_up_by(self, input_text, widgetVal, widgetType, assertVal):
        # 等待手机号输入框可见
        # cons = self.driver.contexts
        func = self.driver.find_element_by_class_name(input_text)
        func.send_keys(widgetVal)
        # 关闭虚拟键盘
        os.popen("adb shell input keyevent 66")

    # 按钮
    def click(self,widgetEle,widgetVal,widgetType,assertVal):
        # 关闭虚拟键盘
        # print(self.driver.contexts)
        time.sleep(1)
        self.get_element_by_common(widgetEle, widgetType).click()
        if assertVal != '' and assertVal != None:
            # BasePage().get_toast_content(assertVal)
            BasePage.toastMsg(self,assertVal)


    # 判断text是否符合预期
    def text(self, widgetEle, widgetVal, widgetType, assertVal):
        textVal = self.get_element_by_common(widgetEle, widgetType).text
        print(textVal)
        assert widgetVal == textVal

    # 判断文本是否存在
    def is_element_exist(self, value):
        # source = self.driver.page_source 获取页面源码
        try:
            ele = self.driver.find_element_by_xpath('//*[contains(@text,"'+value+'")]')
            return ele
        except NoSuchElementException:
            print("没有找到这个元素")
            return False

    # 列表根据元素查找特定数据
    def swipe_up_list(self, widgetEle, widgetVal, widgetType,assertVal):
        size = self.get_window_size()
        self.x = size["width"]
        self.y = size["height"]
        # while循环10次
        i = 0
        while i < 10:
            try:
                flg = Widget.is_element_exist(self, widgetVal)
                if flg != False:
                    flg.click()
                    break
                else:
                    Swipe.swipe_up(self)
                    i = i + 1
            except Exception as e:
                print(e)
                Swipe.swipe_up(self)
                i = i + 1


    # 滑动页面
    def swipe_up(self, widgetEle, widgetVal, widgetType,assertVal):
        size = self.get_window_size()
        self.x = size["width"]
        self.y = size["height"]
        Swipe.swipe_up(self)

    def get_window_size(self):
        """获取窗口大小"""
        return self.driver.get_window_size()

    # 日期框选择
    def date_click(self, widgetEle, widgetVal, widgetType):
        date = widgetVal.split('-')
        if date.length > 0:
            func = self.get_element_by_common(widgetEle, widgetType)
            func.send_keys(widgetVal)

    # 日期框输入
    def input_date(self, input_text, widgetVal, widgetType):
        # 等待手机号输入框可见
        # cons = self.driver.contexts
        js = 'document.getElementById("'+input_text+'").removeAttribute("readonly");'

        self.driver.execute_script(js)
        self.driver.find_element_by_id(input_text).clear()
        self.driver.find_element_by_id(input_text).send_keys("2016-12-25")
        self.driver.find_element_by_id().getText()
        # func = self.get_element_by_common(input_text, widgetType)
        # func.send_keys(widgetVal)
        # 关闭虚拟键盘
        os.popen("adb shell input keyevent 66")

    # 下拉框（不存在就滚动下拉框）
    def select(self, widgetEle, widgetVal, widgetType, assertVal):
        # time.sleep(1)
        forFlg = True

        while forFlg:
            try:
                # 根据xpath获取要定位的下拉框元素
                ele = self.driver.find_element_by_xpath('//*[contains(@text,"' + widgetVal + '")]')
                # 定位到后点击
                ele.click()
                forFlg = False
            except Exception:
                # 获取下拉框
                func = self.get_element_by_common(widgetEle, widgetType)
                # 获取下拉框宽度和高度
                self.x = func.size["width"]
                self.y = func.size["height"]
                # 根据宽高度向上滑动
                Swipe.swipe_up(self)
                time.sleep(1)


    # 根据xpath内容定位元素(不是xpath精准路径，包含文字)
    def click_xpath(self, widgetEle, widgetVal, widgetType, assertVal):
        ele = self.driver.find_element_by_xpath('//*[contains(@text,"' + widgetVal + '")]')
        ele.click()
        if assertVal != '' and assertVal != None:
            BasePage.toastMsg(self,assertVal)

    # 根据横纵坐标向上滚动
    def select_linkage(self, widgetEle, widgetVal, widgetType, assertVal):
        # 获取横纵坐标
        # coordinate = widgetEle.split(',')
        # if coordinate.__len__() != 2:
        #     return '横纵坐标格式有误'

        forFlg = True
        i = 0
        while forFlg and i < 10:
            try:
                # 根据xpath获取要定位的下拉框元素
                ele = self.driver.find_element_by_xpath('//*[contains(@text,"' + widgetVal + '")]')
                # 定位到后点击
                ele.click()
                forFlg = False
            except Exception:
                # 根据宽高度向上滑动
                self.swipe_up('','','','')
                i += 1
                time.sleep(1)

    # 拍图片
    def take_photo(self, widgetEle, widgetVal, widgetType, assertVal):
        self.driver.keyevent(27)

    # 测试case断言用
    def exsit(self, widgetEle, widgetVal, widgetType, assertVal):
        # 关闭虚拟键盘
        # print(self.driver.contexts)
        ele = self.get_element_by_common(widgetEle, widgetType)
        if ele != None:
            # BasePage().get_toast_content(assertVal)
            BasePage.toastMsg(self, assertVal)







